package com.tlcx.kfip.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.tlcx.kfip.R;
import com.tlcx.kfip.adapter.base.BaseAdapter;
import com.tlcx.kfip.entity.IpRecordAlreadyEntity;
import com.tlcx.kfip.utils.AppInfoUtils;

/**
 * Created by wanjixin on 2016/10/13.
 */

public class AlreadyAnnouncedAdapter extends BaseAdapter<IpRecordAlreadyEntity> {
    public AlreadyAnnouncedAdapter(Context context) {
        super(context);
    }
    @Override
    public int getItemResource(int viewType) {
        return R.layout.adapter_already_announced;
    }

    @Override
    public View getItemView(int position, View convertView, ViewHolder viewHolder) {
        IpRecordAlreadyEntity item = getItem(position);
        if (item == null) {
            return convertView;
        }
        //设置icon
        SimpleDraweeView iconIv = viewHolder.getView(R.id.sdv_already_announced_icon);
        Uri uri = Uri.parse("res://" + AppInfoUtils.getCurrentPkgName(mContext) + "/" + item.getIconUrl());
        iconIv.setImageURI(uri);

        //设置标题
        TextView titleTv = viewHolder.getView(R.id.tv_already_announced_title);
        titleTv.setText(item.getTitle());

        //设置总需人次
        TextView infoTv = viewHolder.getView(R.id.tv_already_announced_need);
        infoTv.setText(mContext.getString(R.string.ip_records_tip_need_all)+item.getNeedCounts());

        //设置活动参与情况
        TextView participateCountTv = viewHolder.getView(R.id.tv_already_announced_participated);
        participateCountTv.setText(getParticipateCountStyle(item.getParticipateCounts()));

        //设置中奖者
        TextView winnerTv = viewHolder.getView(R.id.tv_already_announced_winner);
        winnerTv.setText(getWinnerStyle(item.getWinnerName()));

        //设置中奖者号码
        TextView winningNumberTv = viewHolder.getView(R.id.tv_already_announced_winning_number);
        winningNumberTv.setText(getWinningNumberStyle(item.getWinningNumber()));

        //设置本期参与
        TextView participateTv = viewHolder.getView(R.id.tv_already_announced_has_participate);
        participateTv.setText(getParticipateCountStyle(item.getParticipateCounts()));

        //设置揭晓时间
        TextView timeTv = viewHolder.getView(R.id.tv_already_announced_time);
        timeTv.setText(mContext.getString(R.string.ip_records_tip_announced_time)+item.getAnnounceTime());

        //设置中奖者头像
        SimpleDraweeView avatarIv = viewHolder.getView(R.id.sdv_already_announced_avatar);
        avatarIv.setImageURI(uri);

        return convertView;
    }

    /**
     * 设置参与人数样式
     * @param participateCount
     * @return
     */
    private Spanned getParticipateCountStyle(int participateCount){
        StringBuffer buffer = new StringBuffer();
        buffer.append("<font color = '#aaaaaa'>");
        buffer.append(mContext.getString(R.string.ip_records_tip_participate_prefix));
        buffer.append("</font>");
        buffer.append("<font color = '#3fadf9'>");
        buffer.append(participateCount);
        buffer.append("</font>");
        buffer.append("<font color = '#aaaaaa'>");
        buffer.append(mContext.getString(R.string.ip_records_tip_participate_suffix));
        buffer.append("</font>");
        return Html.fromHtml(buffer.toString());
    }
    /**
     * 设置中奖者名称
     * @param winnerName
     * @return
     */
    private Spanned getWinnerStyle(String winnerName){
        StringBuffer buffer = new StringBuffer();
        buffer.append("<font color = '#aaaaaa'>");
        buffer.append(mContext.getString(R.string.ip_records_tip_winner));
        buffer.append("</font>");
        buffer.append("<font color = '#3fadf9'>");
        buffer.append(winnerName);
        buffer.append("</font>");
        return Html.fromHtml(buffer.toString());
    }
    /**
     * 设置中奖号码
     * @param winningNumber
     * @return
     */
    private Spanned getWinningNumberStyle(String winningNumber){
        StringBuffer buffer = new StringBuffer();
        buffer.append("<font color = '#aaaaaa'>");
        buffer.append(mContext.getString(R.string.ip_records_tip_winning_number));
        buffer.append("</font>");
        buffer.append("<font color = '#f1627d'>");
        buffer.append(winningNumber);
        buffer.append("</font>");
        return Html.fromHtml(buffer.toString());
    }
}
